/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package embeddedpaint.user_interface.tools.dialog;

import embeddedpaint.model.layers.ALayer;
import embeddedpaint.model.layers.LayerImage;
import embeddedpaint.model.layers.LayerManager;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.ArrayList;
import java.util.HashMap;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class FrameScalePicture extends ToolFrame
{
    private LayerManager        m_imageManager;
    private JSpinner            m_txtHorizontal             = new JSpinner();
    private JSpinner            m_txtVertical               = new JSpinner();
    private JCheckBox           m_opcionKeepRelation        = new JCheckBox();
    private JButton             m_buttonOK                  = new JButton("OK");
    private JButton             m_buttonCancel              = new JButton("Cancel");
    private JLabel              m_labelHorizontal           = new JLabel("    Horizontal: ");
    private JLabel              m_labelVertical             = new JLabel("    Vertical: ");
    private JLabel              m_labelkeepRelation         = new JLabel("Maintain aspect ratio: ");
    private Dimension           m_dimension                 = new Dimension (200,200);
    private ChangeListener      m_changeVertical;
    private ChangeListener      m_changeHorizontal;
    ArrayList<Dimension>        m_oldDimension              = new ArrayList();
    
    public FrameScalePicture (LayerManager imageManager, JFrame application)
    {    
        this.m_imageManager = imageManager;
        
        this.setTitle("Change size layer");
        this.setVisible(true);
        this.setEnabled(true);
        this.setSize(m_dimension);
        this.setAlwaysOnTop(true);
        this.setAutoRequestFocus(true);
        this.setResizable(false);
        this.setVisible(rootPaneCheckingEnabled);
        this.setLayout(new GridLayout(4,2));
        
        this.add(m_labelHorizontal);
        this.add(m_txtHorizontal);
        this.add(m_labelVertical);
        this.add(m_txtVertical);
        this.add(m_opcionKeepRelation);
        this.add(m_labelkeepRelation);
        this.add(m_buttonOK);
        this.add(m_buttonCancel);
        
        this.initValues();
        this.saveOldValues();
        
        m_buttonCancel.addActionListener(new ActionListener() 
        {
            @Override
            public void actionPerformed (ActionEvent e)
            {
               close();
               setOldValues();       
            }
        });
        
        m_buttonOK.addActionListener(new ActionListener() 
        {
            @Override
            public void actionPerformed (ActionEvent e)
            {
                updateValues();
                close();
            }
        });

        m_changeVertical = new ChangeListener() 
        {
            @Override
            public void stateChanged (ChangeEvent e)
            {
                 m_txtVertical.getModel().setValue((int) m_txtHorizontal.getModel().getValue());
            }
        };
        
        m_changeHorizontal = new ChangeListener() 
        {
             @Override
             public void stateChanged (ChangeEvent e)
             {
                  m_txtHorizontal.getModel().setValue((int) m_txtVertical.getModel().getValue());
             }
        };
       
        m_txtHorizontal.addChangeListener(new ChangeListener()
        {
             @Override
             public void stateChanged (ChangeEvent e)
             {
                 updateValues();
             }
        });
       
        m_txtVertical.addChangeListener(new ChangeListener()
        {
            @Override
            public void stateChanged (ChangeEvent e)
            {
                updateValues();
            }
        });
    
        m_opcionKeepRelation.addActionListener(new ActionListener() 
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                if (m_opcionKeepRelation.isSelected())
                {
                    equalizeValues();
                        
                    m_txtHorizontal.addChangeListener(m_changeVertical);
                    m_txtVertical.addChangeListener(m_changeHorizontal);
                }
                else
                {
                    m_txtHorizontal.removeChangeListener(m_changeVertical);
                    m_txtVertical.removeChangeListener(m_changeHorizontal);
                }
             }
        });
           
        this.pack();
    }
    
    private void equalizeValues (){
        m_txtVertical.getModel().setValue((int) m_txtHorizontal.getModel().getValue());

    }
    
    private void initValues()
    {
        m_txtHorizontal.setModel(new SpinnerNumberModel(100, 0, Integer.MAX_VALUE, 1));
        m_txtVertical.setModel(new SpinnerNumberModel(100, 0, Integer.MAX_VALUE, 1));
    }    
     
    private void updateValues()
    {   int i=0;
        ArrayList<ALayer> selectedImages = this.m_imageManager.getM_selectedImage();
        for (ALayer img : selectedImages)
        {   
            if(img instanceof LayerImage)
            {
                LayerImage imageLayer = (LayerImage) img;
                Dimension d = m_oldDimension.get(i);
                int newHorizontal = (int) (d.width * (Float.parseFloat(""+(int) m_txtHorizontal.getModel().getValue()) / 100));
                int newVertical = (int) (d.height *(Float.parseFloat(""+(int) m_txtVertical.getModel().getValue()) / 100));
                imageLayer.setScale(newHorizontal, newVertical);
                i++;
            }
        }
    }
    
     private void setOldValues (){
      int i = 0;
      ArrayList<ALayer> selectedImages = this.m_imageManager.getM_selectedImage();
        for (ALayer img : selectedImages)
        {   
            if(img instanceof LayerImage)
            {
              LayerImage imageLayer = (LayerImage) img;
              
                Dimension d = m_oldDimension.get(i);
                imageLayer.setScale(d.width, d.height);
                i++;
            }
        }
     }
    
    private void saveOldValues ()
    {
        ArrayList<ALayer> selectedImages = this.m_imageManager.getM_selectedImage();
        for (ALayer img : selectedImages)
        {   
            if(img instanceof LayerImage)
            {
                LayerImage imageLayer = (LayerImage) img;
                Dimension d = new Dimension(img.getM_width(),img.getM_height());
                m_oldDimension.add(d);
            }
        }
    }
    
    public void close()
    {
        this.setEnabled(true);
        this.dispose();
        this.updateObserver();
    }
    
}
        
    
    
    
    
